<template>
  <el-form ref="form" :model="platform" label-width="80px" style="margin:20px;width:60%;min-width:600px;">
    <el-form-item label="平台编码">
      <el-input v-model="platform.no" disabled></el-input>
    </el-form-item>
    <el-form-item label="平台名称">
      <el-input v-model="platform.name"></el-input>
    </el-form-item>
    <el-form-item label="LOGO">
      <el-upload 
        class="avatar-uploader" 
        action="" 
        :show-file-list="false" 
        :before-upload="beforeAvatarUpload">
        <img v-if="platform.icon" :src="platform.icon" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="备注">
      <el-input type="textarea" v-model="platform.remark"></el-input>
    </el-form-item>
    <el-form-item label="管理员账号">
      <el-input v-model="platform.user_code"></el-input>
    </el-form-item>
    <el-form-item label="原密码">
      <el-input v-model="platform.before_password"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="platform.password"></el-input>
    </el-form-item>
    <el-form-item label="确认密码">
      <el-input v-model="platform.re_password"></el-input>
    </el-form-item>
    <el-form-item style="display:none;">
      <el-input type="Number" v-model="platform.id"></el-input>
    </el-form-item>
    <el-form-item style="display:none;">
      <el-input type="Number" v-model="platform.user_id"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click.native="onSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  name: 'test',
  data() {
    return {
      platform: {
        id: 0,
        icon: '',
        name: '',
        no: '',
        remark: '',
        user_id: 0,
        user_code: '',
        logo: undefined
      }
    }
  },
  created() {
    this.$http.post('/api/system/platform/get', { id: 9 }).then(response => {
      this.platform = response.body.data
    })
  },
  methods: {
    beforeAvatarUpload(file) {
      if (typeof FileReader === 'undefined') {
        this.$message.error('您的浏览器不支持图片上传，请升级您的浏览器')
        return false
      }

      var reader = new FileReader()
      reader.readAsDataURL(file)
      var vm = this
      reader.onload = function(e) {
        vm.platform.icon = e.target.result
      }

      this.platform.logo = file
      return false
    },
    onSubmit(event) {
      this.$http.post('/api/system/platform/set', this.platform, {emulateJSON: false}).then(response => {
        console.log(response)
        console.log(response.body)
      })
    }
  }
}
</script>
<style lang='less'>
  .avatar-uploader{
    display: flex;
    line-height: 12px;
    .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      &:hover{
        border-color: #20a0ff;
      }
      .avatar {
        width: 45px;
        height: 45px;
        display: block;
      }
    }
    .avatar-uploader-icon {
      font-size: 12px;
      color: #8c939d;
      width: 45px;
      height: 45px;
      line-height: 45px !important;
      text-align: center;
    }
  }
</style>
